﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NextInventory.Model.Party;
using NextInventory.Business.EntityRepositories;
using System.Web.Security;
using NextInventory.Common;
using System.Web.Script.Serialization;
using MvcJqGrid;

namespace NextInventory.Web.Controllers
{
    [Authorize]
    public class PartyController : Controller
    {
        private IPartyRepository _partyRepository;

        public PartyController(IPartyRepository partyRepository)
        {
            _partyRepository = partyRepository;
        }
        public ActionResult Index()
        {

            return View();
        }

        public ActionResult Party()
        {
            return View();
        }
        public JsonResult PartyGrid(GridSettings gridSettings)
        {
            int totalRecords;
            PartyViewModel filter = new PartyViewModel();
            if (gridSettings.IsSearch)
            {
                filter.Name = gridSettings.Where.rules.Any(r => r.field == "Name") ?
                    gridSettings.Where.rules.FirstOrDefault(r => r.field == "Name").data : string.Empty;
                filter.Phone = gridSettings.Where.rules.Any(r => r.field == "Phone") ?
                    gridSettings.Where.rules.FirstOrDefault(r => r.field == "Phone").data : string.Empty;
                filter.Email = gridSettings.Where.rules.Any(r => r.field == "Email") ?
                    gridSettings.Where.rules.FirstOrDefault(r => r.field == "Email").data : string.Empty;

                var creditLimit = gridSettings.Where.rules.Any(r => r.field == "CreditLimit") ? gridSettings.Where.rules.FirstOrDefault(r => r.field == "CreditLimit").data : string.Empty;
                decimal dCreditLimit = 0;
                decimal.TryParse(creditLimit, out dCreditLimit);
                filter.CreditLimit = dCreditLimit;

                var balance = gridSettings.Where.rules.Any(r => r.field == "Balance") ? gridSettings.Where.rules.FirstOrDefault(r => r.field == "Balance").data : string.Empty;
                decimal dbalance = 0;
                decimal.TryParse(balance, out dbalance);
                filter.Balance = dbalance;
                filter.Address = gridSettings.Where.rules.Any(r => r.field == "Address") ?
                    gridSettings.Where.rules.FirstOrDefault(r => r.field == "Address").data : string.Empty;
                //filter.ModeOfPayment = gridSettings.Where.rules.Any(r => r.field == "ModeOfPayment") ? gridSettings.Where.rules.FirstOrDefault(r => r.field == "ModeOfPayment").data : null;
                //filter.PartyStatus = gridSettings.Where.rules.Any(r => r.field == "PartyStatus") ?
                //  gridSettings.Where.rules.FirstOrDefault(r => r.field == "PartyStatus").data : string.Empty;
            }

            var party = _partyRepository.GetAllParties(filter, gridSettings.SortColumn, gridSettings.SortOrder, gridSettings.PageSize, gridSettings.PageIndex, out totalRecords);
            var totalParty = party.Count();

            var jsonData = new
            {
                total = totalParty / gridSettings.PageSize + 1,
                page = gridSettings.PageIndex,
                records = totalParty,
                rows = (
                    from p in party
                    select new
                    {
                        ID = p.ID,
                        Name = p.Name,
                        Phone = p.Phone,
                        Email = p.Email,
                        CreditLimit = p.CreditLimit,
                        Balance = p.Balance,
                        Address = p.Address,
                        ModeOfPayment = p.ModeOfPayment,
                        //PartyStatus = p.PartyStatus
                    })
            };

            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Create()
        {
            // ViewBag.mod = new SelectList(_partyRepository.GetModeOfPaymentList(), "ModeOfPaymentId", "Name");
            ViewBag.xyz = "abc";
            return View();
        }
        [HttpPost]
        public ActionResult Create(PartyModel model)
        {
            string message = string.Empty;
            if (ModelState.IsValid)
            {
                if (_partyRepository.AddParty(model, out message))
                {
                    return RedirectToAction("Create", "Party");
                }
                else
                {
                    ModelState.AddModelError("", message);
                }
            }
            return View(model);
        }

        public ActionResult Delete(int id)
        {
            if (_partyRepository.DeleteParty(id))
            {
                return RedirectToAction("Index", "Party");
            }
            return View();
        }
    }
}
